package com.web.base.open.service;

import com.blue.jdbc.JdbcDataSource;
import com.web.base.open.dao.RoleUrlDao;
import com.web.base.open.model.RoleUrl;
import com.web.base.open.model.Url;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 开放平台角色-链接业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年02月27日
 */
@Service("openRoleUrlService")
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class RoleUrlService
{
	@Autowired
	private RoleUrlDao roleUrlDao;
	
	public RoleUrlService()
	{
	}
	
	public List<Url> listSelect(Integer roleId)
	{
		List<Url> urlList = roleUrlDao.listSelect(roleId);
		return urlList;
	}
	
	public void save(RoleUrl roleUrl)
	{
		roleUrlDao.deleteByRole(roleUrl.getRoleId());
		
		if (roleUrl.getUrlIds() != null && !roleUrl.getUrlIds().isEmpty())
		{
			List<RoleUrl> roleMenuList = new ArrayList<>();
			for (String urlId : roleUrl.getUrlIds().split(","))
			{
				roleMenuList.add(new RoleUrl(roleUrl.getRoleId(), Integer.valueOf(urlId)));
			}
			roleUrlDao.saveList(roleMenuList);
		}
		
	}
	
	public List<String> listByRole(Integer roleId)
	{
		return roleUrlDao.listByRole(roleId);
	}
	
	public List<RoleUrl> list(RoleUrl roleUrl)
	{
		return roleUrlDao.list(roleUrl);
	}
	
}
